package com.reds.backend.controller.addressController;

import com.alibaba.fastjson2.JSONObject;
import com.reds.backend.common.ApiResponse;
import com.reds.backend.service.address.addressCRUDService;
import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
    @RequestMapping("/address")
    public class addressCRUDController {
        @Autowired
        private addressCRUDService addressCRUDService;
    // 增加地址
        @PostMapping("/add/")
        public ApiResponse<String> addAddress(@RequestParam Map<String, String> map) {
            StpAdminUtil.checkRoleOr("admin", "superAdmin");
            int aid = Integer.parseInt(map.get("aid"));
            String address = map.get("address");
            return ApiResponse.success(addressCRUDService.addAddress(aid, address));
        }
    // 删除地址
        @PostMapping("/detele/")
        public ApiResponse<String> deleteAddress(@RequestParam Map<String, String> map) {
            StpAdminUtil.checkRoleOr("admin", "superAdmin");
            return ApiResponse.success(addressCRUDService.deteleAddress(Integer.parseInt(map.get("id"))));
        }
    // 更新地址
        @PostMapping("/update/")
        public ApiResponse<String> updateAddress(@RequestParam Map<String, String> map) {
            StpAdminUtil.checkRoleOr("admin", "superAdmin");
            return ApiResponse.success(addressCRUDService.updataAddress(Integer.parseInt(map.get("aid")), map.get("address")));
        }
        @PostMapping("/getAddress/")
        public ApiResponse<String> getAddress(@RequestParam Map<String, String> map) {
            return ApiResponse.success(addressCRUDService.getAddress(Integer.parseInt(map.get("aid"))));
        }

    // 按 aid 返回对应的文章和地址
        @PostMapping("/getArticleByAddress/")
        public ApiResponse<JSONObject> getAddressByAid(@RequestParam Map<String, String> map) {
            // 根据ADDRESS查文章
           return ApiResponse.success(addressCRUDService.getArticleByAid(map.get("address"), Integer.parseInt(map.get("page")), Integer.parseInt(map.get("sizes"))));
        }
    }


